package com.liu.eduService.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.eduService.Excel.ExcelData;
import com.liu.eduService.Excel.ExcelListener;
import com.liu.eduService.entity.EduSubject;
import com.liu.eduService.entity.subject.oneSubject;
import com.liu.eduService.entity.subject.twoSubject;
import com.liu.eduService.mapper.EduSubjectMapper;
import com.liu.eduService.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.serviceBase.exception.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author LSJ
 * @since 2022-01-06
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void importSubject(MultipartFile file, EduSubjectService service) {
        InputStream inputStream = null;
        // 这次根据文件流来传递Excel文件

        try {
            inputStream = file.getInputStream();
            EasyExcel.read(inputStream, ExcelData.class,new ExcelListener(service)).sheet().doRead();


        } catch (IOException e) {
            e.printStackTrace();
            throw new MyException(20002,"添加课程分类失败");
        }

    }

    @Override
    public List<oneSubject> getSubject() {
        ArrayList<oneSubject> oneSubjectsLast = new ArrayList<>(); // 最终结果

//        1\ 获取所有一级标题
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        List<EduSubject> subjectList = baseMapper.selectList(wrapper);
        //          2、获取所有二级标题
        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        wrapper2.ne("parent_id","0");
        List<EduSubject> subjectList2 = baseMapper.selectList(wrapper2);

//        将一级标题subjectList中的类 转换成 oneSubject类
        for (int i = 0; i <subjectList.size() ; i++) {
            oneSubject oneSubject = new oneSubject();

            EduSubject eduSubject = subjectList.get(i);
            BeanUtils.copyProperties(eduSubject,oneSubject);
            oneSubjectsLast.add(oneSubject);

//        每次循环 使用children list记录符合条件的
            List<twoSubject> children =new ArrayList<> ();
            for (int j = 0; j <subjectList2.size() ; j++) {
                // 判断此时二级分类的pid与一级的id是否一致，是则添加
                EduSubject eduSubject2 = subjectList2.get(j);
//                System.out.println("************"+eduSubject2);

                if (eduSubject2.getParentId().equals(eduSubject.getId())){
                    // 先将二级的转换成 twosubject
                    twoSubject twoSubject = new twoSubject();
                    BeanUtils.copyProperties(eduSubject2,twoSubject);
//                    System.out.println("************"+eduSubject2);
                    children.add(twoSubject);

                }

            }
//            System.out.println("**"+children);

            oneSubject.setChildren(children);

        }
//        3、将所有的一级标题遍历

//        4、遍历二级标题判断并加入到所存在的一级标题列表中
        return oneSubjectsLast;
    }
}
